{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movieId</th>\n",
       "      <th>title</th>\n",
       "      <th>genres</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Toy Story (1995)</td>\n",
       "      <td>Adventure|Animation|Children|Comedy|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jumanji (1995)</td>\n",
       "      <td>Adventure|Children|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Grumpier Old Men (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Waiting to Exhale (1995)</td>\n",
       "      <td>Comedy|Drama|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Father of the Bride Part II (1995)</td>\n",
       "      <td>Comedy</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   movieId                               title  \\\n",
       "0        1                    Toy Story (1995)   \n",
       "1        2                      Jumanji (1995)   \n",
       "2        3             Grumpier Old Men (1995)   \n",
       "3        4            Waiting to Exhale (1995)   \n",
       "4        5  Father of the Bride Part II (1995)   \n",
       "\n",
       "                                        genres  \n",
       "0  Adventure|Animation|Children|Comedy|Fantasy  \n",
       "1                   Adventure|Children|Fantasy  \n",
       "2                               Comedy|Romance  \n",
       "3                         Comedy|Drama|Romance  \n",
       "4                                       Comedy  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "movie = pandas.read_csv('Data/movies.csv')\n",
    "movie.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "movie_dic = {}\n",
    "for rec in movie.iterrows():\n",
    "    movie_dic[rec[1].movieId]  = rec[1].title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Toy Story (1995)'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_dic.get(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 100004 entries, 0 to 100003\n",
      "Data columns (total 4 columns):\n",
      "userId       100004 non-null int64\n",
      "movieId      100004 non-null int64\n",
      "rating       100004 non-null float64\n",
      "timestamp    100004 non-null int64\n",
      "dtypes: float64(1), int64(3)\n",
      "memory usage: 3.1 MB\n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "import datetime\n",
    "\n",
    "df = pandas.read_csv('Data/ratings.csv')\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = df[df['timestamp'] >= 1325376000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 20877 entries, 790 to 99258\n",
      "Data columns (total 4 columns):\n",
      "userId       20877 non-null int64\n",
      "movieId      20877 non-null int64\n",
      "rating       20877 non-null float64\n",
      "timestamp    20877 non-null int64\n",
      "dtypes: float64(1), int64(3)\n",
      "memory usage: 815.5 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from apyori import apriori\n",
    "transactions = [ele for ele in df.groupby('userId')['movieId'].apply(list)]\n",
    "rules = apriori(transactions, min_support = 0.2, min_confidence = 0.5, min_lift = 3, min_length = 2)\n",
    "\n",
    "results = list(rules)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kill Bill: Vol. 1 (2003);Kill Bill: Vol. 2 (2004)\n",
      "Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Star Wars: Episode V - The Empire Strikes Back (1980);Raiders of the Lost Ark (Indiana Jones and the Raiders of the Lost Ark) (1981);Star Wars: Episode VI - Return of the Jedi (1983)\n",
      "Lord of the Rings: The Two Towers, The (2002);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Star Wars: Episode V - The Empire Strikes Back (1980);Star Wars: Episode VI - Return of the Jedi (1983)\n"
     ]
    }
   ],
   "source": [
    "for rec in results:    \n",
    "    print(';'.join([movie_dic.get(item) for item in rec.items]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pymining in c:\\programdata\\anaconda3\\lib\\site-packages\n"
     ]
    }
   ],
   "source": [
    "! pip install pymining"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pymining import itemmining\n",
    "fp_input = itemmining.get_fptree(transactions)\n",
    "report = itemmining.fpgrowth(fp_input, min_support=30, pruning=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Batman Begins (2005);Matrix, The (1999);Lord of the Rings: The Return of the King, The (2003);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Batman Begins (2005);Lord of the Rings: The Return of the King, The (2003);Inception (2010);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Star Wars: Episode V - The Empire Strikes Back (1980);Lord of the Rings: The Return of the King, The (2003);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Star Wars: Episode V - The Empire Strikes Back (1980);Lord of the Rings: The Return of the King, The (2003);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Star Wars: Episode V - The Empire Strikes Back (1980);Lord of the Rings: The Return of the King, The (2003)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Matrix, The (1999);Star Wars: Episode V - The Empire Strikes Back (1980);Lord of the Rings: The Return of the King, The (2003);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Star Wars: Episode V - The Empire Strikes Back (1980);Lord of the Rings: The Return of the King, The (2003);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Lord of the Rings: The Return of the King, The (2003);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Lord of the Rings: The Return of the King, The (2003);Inception (2010);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Lord of the Rings: The Return of the King, The (2003);Inception (2010);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Lord of the Rings: The Return of the King, The (2003);Inception (2010)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Matrix, The (1999);Lord of the Rings: The Return of the King, The (2003);Inception (2010);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Two Towers, The (2002);Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Inception (2010);Dark Knight, The (2008)\n",
      "Lord of the Rings: The Fellowship of the Ring, The (2001);Star Wars: Episode IV - A New Hope (1977);Matrix, The (1999);Lord of the Rings: The Return of the King, The (2003);Inception (2010);Dark Knight, The (2008)\n"
     ]
    }
   ],
   "source": [
    "for ele in report:\n",
    "    if len(ele) >=6:\n",
    "        print(';'.join([movie_dic.get(item) for item in ele]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
